package com.kaoyaya.delivery.fragment;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.ViewById;
import com.kaoyaya.delivery.activity.R;
import com.kaoyaya.delivery.activity.SlidingActivity;
import com.kaoyaya.delivery.adapter.CostRecordAdapter;
import com.kaoyaya.delivery.data.Constants;
import com.kaoyaya.delivery.data.DataHelper;
import com.kaoyaya.delivery.data.entity.CostRecord;
import com.kaoyaya.util.CommUtil;

import android.app.Dialog;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;

@EFragment(R.layout.fmt_cost_record)
public class CostRecordFmt extends Fragment {
	
	@ViewById TextView tvTitle;
	@ViewById ListView lvCostRecord;
	@Bean DataHelper dataHelper;
	CostRecordAdapter costRecordAdapter;
	SlidingActivity activity;
	Dialog pgDialog;
	final int IS_COST_RECORD = 0;
	
	// 更多布局
	LinearLayout llMore;
	ProgressBar pbMore;
	TextView tvMore;
	boolean hasMore;
	int lastIndex, count, pageIndex;
	
	@AfterViews
	void initView() {
		tvTitle.setText("消费记录");
		activity = (SlidingActivity) getActivity();
		
		View vMore = LayoutInflater.from(activity).inflate(R.layout.more_data_bar, null);
		llMore = (LinearLayout) vMore.findViewById(R.id.llMore);
		llMore.setVisibility(View.GONE);
		pbMore = (ProgressBar) vMore.findViewById(R.id.pbMore);
		tvMore = (TextView) vMore.findViewById(R.id.tvMore);
		llMore.setOnClickListener(clickListener);
		lvCostRecord.addFooterView(vMore);
		lvCostRecord.setOnScrollListener(scrollListener);
		dataHelper.setCallbackHandler(callbackHandler);
		initRecord();
	}
	
	void initRecord() {
		pgDialog = CommUtil.createLoadingDialog(activity, null);
		lastIndex = 0; count = 0; pageIndex = 0;
		costRecordAdapter = null;
		dataHelper.getCostRecords(IS_COST_RECORD, pageIndex);
	}
	
	void nextPage() {
		if (hasMore) {
			pbMore.setVisibility(View.VISIBLE);
			tvMore.setText("数据正在加载，请稍候...");
			pageIndex++;
			dataHelper.getCostRecords(IS_COST_RECORD, pageIndex);
		}
	}
	
	View.OnClickListener clickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.llMore:
				nextPage();
				break;
			}
		}
	};

	OnScrollListener scrollListener = new OnScrollListener() {

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			lastIndex = firstVisibleItem + visibleItemCount - 1;
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			if (hasMore && lastIndex == costRecordAdapter.getCount() && scrollState == OnScrollListener.SCROLL_STATE_FLING) {
				nextPage();
			}
		}
		
	};
	
	@SuppressWarnings("unchecked")
	Handler callbackHandler = new Handler() {
		public void handleMessage(Message msg) {
			Object result = msg.obj;
			switch (msg.what) {
			case IS_COST_RECORD:
				if (costRecordAdapter == null) {
					costRecordAdapter = new CostRecordAdapter(getActivity(), new ArrayList<CostRecord>());
					lvCostRecord.setAdapter(costRecordAdapter);
				}
				List<CostRecord> records = (List<CostRecord>) result;
				int size = records.size();
				count += size;
				costRecordAdapter.addRecord(records);
				costRecordAdapter.notifyDataSetChanged();
				hasMore = size < Constants.PAGE_DEFAULT_SIZE ? false : true;
				if (count > 0) {
					tvMore.setText(hasMore ? "加载更多..." : "总共" + count + "个记录");
					pbMore.setVisibility(View.GONE);
					llMore.setVisibility(View.VISIBLE);
				} else {
					Toast.makeText(activity, "没有获取到对应的数据!", Toast.LENGTH_SHORT).show();
				}
				break;
			}
			if (pgDialog != null) {
				pgDialog.cancel();
			}
		}
	};
	
}
